#include "TuxedoSite.h"

CTuxedoSite::CTuxedoSite(void)
{
	m_sock = new wxSocketClient( );
	
#if 0	
	m_sock->SetEventHandler(*this, SOCKET_ID);
	m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG);
	m_sock->Notify(true);
#endif

	m_sock->Notify(false);

	b_Verified = false;
	b_Connected = false;
}

CTuxedoSite::~CTuxedoSite(void)
{
}

void CTuxedoSite::InitSocket(wxEvtHandler& handler, int id)
{
	m_sock->SetEventHandler(handler, id);
	m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG);
	m_sock->Notify(true);

}

int CTuxedoSite::TestConnect()
{
	if( m_sock->IsConnected() && m_sock->IsOk() ) return 1;

	else return -1;
}

int CTuxedoSite::Connect(wxString IP, wxInt16 Port)
{

	wxIPV4address addr;

	addr.Hostname(IP);
	addr.Service(Port);

	m_sock->Connect(addr, false);
	m_sock->WaitOnConnect(15);

	if( m_sock->IsConnected() ){
		;
	} else {
		m_sock->Close();
		return -1;
	}

	//m_sock->SetFlags(wxSOCKET_WAITALL);
	m_sock->SetTimeout(30);

	return 1;
}

int CTuxedoSite::HandShake()
{
	unsigned char c = HANDINIT; //Current Version
	unsigned char d = 0;

	if( ! m_sock->IsConnected() )
		return -1;
	
	m_sock->Write(&c, 1);

	m_sock->Read(&d, 1);

	if( d !=  HANDACK ) { //Version Miscompatiable.
		m_sock->Close();
		b_Verified = false;
		return -1;
	}

	b_Verified = true;

	return 1;
}


wxString CTuxedoSite::ActVsAgent( int flag, wxString xmlbuf)
{
	unsigned char c = flag;
	wxChar buflen[10];
	wxUint32 len;
	wxUint32 realread = 0; 
	wxUint32 totalread = 0;
	wxChar* rcvmsg;
	const wxChar* sndmsg = xmlbuf.fn_str();
	wxString data = wxT("");

//	if( ! b_Verified ) return NULL;
	try {	
		m_sock->Write(&c, 1); //Write Flag

		if( (len = (wxUint32)strlen(sndmsg)) != 0 ) { //WebLogic Send InitBuff
			memset( buflen, 0, sizeof(buflen) );
			sprintf( buflen, "% 8d", len );
			m_sock->Write( buflen, (wxUint32)strlen(buflen) );
			m_sock->Write( sndmsg, (wxUint32)strlen(sndmsg) );
			totalread = m_sock->LastCount();
			if( totalread == 0  && m_sock->Error() ) {
				return data;
			}
		}

		memset( buflen, 0, sizeof(buflen) );
		m_sock->Read(buflen, 9);
		totalread = m_sock->LastCount();
		if( totalread == 0  && m_sock->Error() ) {
			return data;
		}
		while( totalread < 9 )  {
			m_sock->Read(buflen+totalread, 9-totalread);		
			realread = m_sock->LastCount();
			if( m_sock->Error() ) break;
			totalread += realread;
		}
		len = wxAtoi(buflen);

		rcvmsg = new wxChar[len + 1];

		m_sock->Read(rcvmsg, len);
		totalread = m_sock->LastCount();
		if( totalread == 0  && m_sock->Error() ) {
			delete rcvmsg;
			return data;
		}
		while( totalread < len )  {
			m_sock->Read(rcvmsg+totalread, len-totalread);		
			realread = m_sock->LastCount();
			if( m_sock->Error() ) break;
			totalread += realread;
		}
		rcvmsg[totalread] = '\0';

		data = rcvmsg;
		delete rcvmsg;
	} catch ( ... ) {
		 return wxT("");
	}

	return data;
}

int CTuxedoSite::Close()
{
	unsigned char c = CLOSECONNECT;
	
//	if( ! b_Verified ) return NULL;

	m_sock->Write(&c, 1);

	m_sock->Close();
	return 1;
}
int CTuxedoSite::OnEvent(wxSocketEvent& event)
{
	wxSocketBase *sock = event.GetSocket();
	int socketflag = event.GetSocketEvent();
	switch( socketflag )
	{
	case wxSOCKET_CONNECTION:
		break;
	case wxSOCKET_INPUT:
		break;
	case wxSOCKET_LOST:
		m_sock->Close();
		//sock->Destroy();
		break;
	}
	return socketflag;
}